let xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest;
} else {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
function uplaod() {
    let formdata;
    let msg = document.getElementById('msg');
    let lid = document.getElementById('lid').value;
    let family_id = document.getElementById('family_id').value
    let title = document.getElementById('title').value;
    let subtitle = document.getElementById('subtitle').value;
    let price = document.getElementById('price').value;
    let promise = document.getElementById('promise').value;
    let spec = document.getElementById('spec').value;
    let lname = document.getElementById("lname").value;
    let os = document.getElementById('os').value;
    let memory = document.getElementById('memory').value;
    let resolution = document.getElementById('resolution').value;
    let video_card = document.getElementById('video_card').value;
    let cpu = document.getElementById('cpu').value;
    let video_memory = document.getElementById("video_memory").value;
    let category = document.getElementById('category').value;
    let disk = document.getElementById('disk').value;
    let details = document.getElementById('details').value;
    let shelf_time = document.getElementById('shelf_time').value;
    let sold_count = document.getElementById('sold_count').value;
    let is_onsale = document.getElementById('is_onsale').value;

    xmlhttp.onreadystatechange = () => {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            msg.innerHTML = JSON.parse(xmlhttp.responseText).msg;
        }
    }
    xmlhttp.open('POST', 'http://127.0.0.1:8080/v1/laptops/upload', true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    formdata = `lid=${lid}&family_id=${family_id}&title=${title}&subtitle=${subtitle}&`;
    formdata += `price=${price}&promise=${promise}&spec=${spec}&lname=${lname}&os=${os}&`;
    formdata += `memory=${memory}&resolution=${resolution}&video_card=${video_card}&cpu=${cpu}&`;
    formdata += `video_memory=${video_memory}&category=${category}&disk=${disk}&details=${details}&`;
    formdata += `shelf_time=${shelf_time}&sold_count=${sold_count}&is_onsale=${is_onsale}`;
    xmlhttp.send(formdata);
}

/* if(!confirm("是否删除编号为"+lid+"的信息")){
        return;
    } */
function showLMsg() {
    let html = "";
    let laptopmsg=document.getElementById('laptopmsg');
    xmlhttp.onreadystatechange = () => {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            let laptops = JSON.parse(xmlhttp.responseText);
            // console.log('laptops: ', laptops);
            for (let i = 0; i < laptops.length; i++) {
                html += `<tr>
                    <td>${laptops[i].lid}</td>
                    <td>${laptops[i].family_id}</td>
                    <td>${laptops[i].title}</td>
                    <td>${laptops[i].subtitle}</td>
                    <td>${laptops[i].price}</td>
                    <td>${laptops[i].promise}</td>
                    <td>${laptops[i].spec}</td>
                    <td>${laptops[i].lname}</td>
                    <td>${laptops[i].os}</td>
                    <td>${laptops[i].memory}</td>
                    <td>${laptops[i].resolution}</td>
                    <td>${laptops[i].video_card}</td>
                    <td>${laptops[i].cpu}</td>
                    <td>${laptops[i].video_memory}</td>
                    <td>${laptops[i].category}</td>
                    <td>${laptops[i].disk}</td>
                    <td>${laptops[i].details}</td>
                    <td>${laptops[i].shelf_time}</td>
                    <td>${laptops[i].sold_count}</td>
                    <td>${laptops[i].is_onsale}</td>
                    <td><a href="javascript:deleL(${laptops[i].lid})">删除</a></td>
                    </tr>`;
            }
            laptopmsg.innerHTML=html;
        }
    }
    xmlhttp.open('GET', "http://127.0.0.1:8080/v1/laptops/list", true);
    xmlhttp.send();
}

function deleL(lid){
    if(!confirm('是否要删除编号为'+lid+"的信息")){
        return;
    }
    xmlhttp.onreadystatechange=()=>{
       if(xmlhttp.readyState===4&&xmlhttp.status==200){
           let sta=JSON.parse(xmlhttp.responseText).code;
           if(sta==200){
               showLMsg();
               alert("编号为"+lid+"的信息删除成功！")
           }else{
            alert("编号为"+lid+"的信息删除失败！")
           }
       }
    }
    let url="http://127.0.0.1:8080/v1/laptops/delete"+lid;
    xmlhttp.open('DELETE',url,true);
    xmlhttp.send();
}